package com.lonely.factory.simple;

import org.apache.commons.lang3.EnumUtils;

import java.util.Arrays;
import java.util.Objects;

/**
 * @date: 2022-03-29 14:14
 * @author: ztkj-hzb
 * @description: 动物工厂
 **/
public class AnimalFactory {

    /**
     * 动物类型枚举
     */
    public enum AnimalTypeEnum {

        /**
         * 猫
         */
        CAT("cat"),

        /**
         * 狗
         */
        DOG("dog");

        /**
         * 类型
         */
        public String type;

        AnimalTypeEnum(String type) {
            this.type = type;
        }

        /**
         * 根据type获取对应的枚举
         * @param type
         * @return
         */
        public static AnimalTypeEnum getInstance(String type) {
            return Arrays.stream(AnimalTypeEnum.values()).filter(x -> Objects.equals(x.type, type))
                    .findFirst().orElseThrow(() -> new RuntimeException("找不到对应的枚举"));
        }

    }

    /**
     * 根据类型获取具体的实例
     *
     * @param type
     * @return
     */
    public static Animal getAnimal(String type) {
        AnimalTypeEnum animalTypeEnum = AnimalTypeEnum.getInstance(type);
        switch (animalTypeEnum) {
            case CAT:
                return new Cat();
            case DOG:
                return new Dog();
            default:
                break;
        }

        return null;

    }

}
